﻿package com.oaxoa.components
{
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import flash.utils.*;

    public class FrameRater extends Sprite
    {
        private var _graphBox:Sprite;
        private var _text:TextField;
        private var _tf:TextFormat;
        private var _graphColor:uint;
        private var _graph:Sprite;
        private var _timer:Timer;
        private var _dropShadow:DropShadowFilter;
        private var _graphCounter:uint;
        private var _showGraph:Boolean;
        private var _c:uint = 0;
		/***
         **/
        public function FrameRater(param1:uint = 0, param2:Boolean = false, param3:Boolean = true, param4:uint = 16711680)
        {
            _c = 0;
            _showGraph = param3;
            _graphColor = param4;
            if (_showGraph)
            {
                initGraph();
            }
            _dropShadow = new DropShadowFilter(1, 90, 0, 1, 2, 2);
            _tf = new TextFormat();
            _tf.color = param1;
            _tf.font = "_sans";
            _tf.size = 11;
            _text = new TextField();
            _text.width = 100;
            _text.height = 20;
            _text.x = 3;
            if (param2)
            {
                _text.filters = [_dropShadow];
            }
            addChild(_text);
            _timer = new Timer(500);
            _timer.addEventListener(TimerEvent.TIMER, onTimer);
            _timer.start();
            addEventListener(Event.ENTER_FRAME, onFrame);
            return;
        }// end function

        public function updateGraph(param1:Number) : void
        { 
			var _loc_3:*
            if (_graphCounter > 30)
            {
                var _loc_2:* = _graph;
                _loc_3 = _graph.x - 1;
                _loc_2.x = _loc_3;
            }
			_loc_3 = _graphCounter + 1;
            _graphCounter = _loc_3;
            _graph.graphics.lineTo(_graphCounter, 1 + (stage.frameRate - param1) / 3);
            return;
        }// end function

        private function initGraph() : void
        {
            _graphCounter = 0;
            _graph = new Sprite();
            _graphBox = new Sprite();
            _graphBox.graphics.beginFill(16711680);
            _graphBox.graphics.drawRect(0, 0, 36, 100);
            _graphBox.graphics.endFill();
            _graph.mask = _graphBox;
            var _loc_1:int = 5;
            _graphBox.x = 5;
            _graph.x = _loc_1;
             _loc_1 = 20;
            _graphBox.y = 20;
            _graph.y = _loc_1;
            _graph.graphics.lineStyle(1, _graphColor);
            addChild(_graph);
            addChild(_graphBox);
            return;
        }// end function

        private function onTimer(event:TimerEvent) : void
        {
            var _loc_2:Number = NaN;
            _loc_2 = computeTime();
            _text.text = Math.floor(_loc_2).toString() + " fps";
            _text.setTextFormat(_tf);
            _text.autoSize = "left";
            if (_showGraph)
            {
                updateGraph(_loc_2);
            }
            return;
        }// end function

        private function onFrame(event:Event) : void
        {
            var _loc_3:* = _c + 1;
            _c = _loc_3;
            return;
        }// end function

        public function computeTime() : Number
        {
            var _loc_1:uint = 0;
            _loc_1 = _c;
            _c = 0;
            return _loc_1 * 2 - 1;
        }// end function

    }
}
